package com.example.prjbackend.mapper;

import com.example.prjbackend.domain.YuangongQueryParam;
import com.example.prjbackend.domain.Yuanggong;
import com.example.prjbackend.domain.YuanggongDetail;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface YuangongMapper
{
    @Select("<script>" +
            "select y.yid,y.yname,y.ydescp,y.photoUrl,y.age,y.rz_date,y.b_id,b.bname bumengName " +
            "from yuangong y left join bumeng b on y.b_id=b.bid " +
            "<where>" +
            // 仅当b_id不为null且不等于0时才拼接条件
            "<if test='b_id != null and b_id != 0'>and b_id=#{b_id} </if>" +
            // 仅当yname不为null且不为空字符串时才拼接条件
            "<if test='yname != null and yname != \"\"'>and yname like concat('%',#{yname},'%') </if>" +
            "<if test='ydescp != null and ydescp != \"\"'>and ydescp like concat('%',#{ydescp},'%') </if>" +
            "<if test='age1 != null and age2 != null'>and age between #{age1} and #{age2} </if>" +
            "</where>" +
            "</script>")
    List<YuanggongDetail> list(YuangongQueryParam param);

    @Insert("insert into yuangong(yname,ydescp,photoUrl,age,rz_date,b_id)" +
            "values(#{yname},#{ydescp},#{photoUrl},#{age},#{rz_date},#{b_id})")
    int add(Yuanggong yuanggong);

    @Update("<script>" +
            "update yuangong " +
            "<set>" +
            "  <if test='yname!=null'>yname=#{yname},</if>" +
            "  <if test='ydescp!=null'>ydescp=#{ydescp},</if>" +
            "  <if test='photoUrl!=null'>photoUrl=#{photoUrl},</if>" +
            "  <if test='age!=null'>age=#{age},</if>" +
            "  <if test='rz_date!=null'>rz_date=#{rz_date},</if>" +
            "  <if test='b_id!=null'>b_id=#{b_id},</if>" +
            "</set>" +
            " where yid=#{yid}" +
            "</script>")
    int edit(Yuanggong yuanggong);

    @Delete("<script>" +
            "delete from yuangong where yid in " +
            "<foreach collection='array' item='yid' open='(' close=')' separator=','>" +
            "#{yid}" +
            "</foreach>" +
            "</script>")
    int delete(Long[] ids);

}
